using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(CharacterSystem))]
public class CharacterLiving : NetworkBehaviour
{
	[SyncVar]
	public byte Hungry = 100;

	public byte HungryMax = 100;

	[SyncVar]
	public byte Water = 100;

	public byte WaterMax = 100;

	[HideInInspector]
	public CharacterSystem character;

	private static int kCmdCmdEatUpdate;

	private static int kCmdCmdDrinkUpdate;

	private static int kCmdCmdHealthUpdate;

	public byte NetworkHungry
	{
		get
		{
			return Hungry;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref Hungry, 1u);
		}
	}

	public byte NetworkWater
	{
		get
		{
			return Water;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref Water, 2u);
		}
	}

	[HideInInspector]
	private void Start()
	{
		character = GetComponent<CharacterSystem>();
		InvokeRepeating("stomachUpdate", 1f, 1f);
		InvokeRepeating("hungryUpdate", 1f, 15f);
		InvokeRepeating("thirstilyUpdate", 1f, 10f);
	}

	public void Respawn()
	{
		NetworkHungry = HungryMax;
		NetworkWater = WaterMax;
	}

	private void Update()
	{
		if (Hungry < 0)
		{
			NetworkHungry = 0;
		}
		if (Hungry > HungryMax)
		{
			NetworkHungry = HungryMax;
		}
		if (Water < 0)
		{
			NetworkWater = 0;
		}
		if (Water > WaterMax)
		{
			NetworkWater = WaterMax;
		}
	}

	public void stomachUpdate()
	{
		if (base.isServer && !(character == null) && (!character || character.IsAlive))
		{
			if (Water <= 0)
			{
				character.ApplyDamage(2, Vector3.up, character.NetID, 0);
			}
			if (Hungry <= 0)
			{
				character.ApplyDamage(1, Vector3.up, character.NetID, 0);
			}
		}
	}

	public void hungryUpdate()
	{
		if (base.isServer)
		{
			NetworkHungry = (byte)(Hungry - 1);
		}
	}

	public void thirstilyUpdate()
	{
		if (base.isServer)
		{
			NetworkWater = (byte)(Water - 1);
		}
	}

	[Command]
	private void CmdEatUpdate(byte num)
	{
		NetworkHungry = (byte)(Hungry + num);
	}

	public void Eating(byte num)
	{
		CallCmdEatUpdate(num);
	}

	[Command]
	private void CmdDrinkUpdate(byte num)
	{
		NetworkWater = (byte)(Water + num);
	}

	public void Drinking(byte num)
	{
		CallCmdDrinkUpdate(num);
	}

	[Command]
	private void CmdHealthUpdate(byte num)
	{
		if (!(character == null))
		{
			CharacterSystem characterSystem = character;
			characterSystem.NetworkHP = (byte)(characterSystem.HP + num);
		}
	}

	public void Healing(byte num)
	{
		if (!(character == null))
		{
			CallCmdHealthUpdate(num);
		}
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeCmdCmdEatUpdate(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdEatUpdate called on client.");
		}
		else
		{
			((CharacterLiving)obj).CmdEatUpdate((byte)reader.ReadPackedUInt32());
		}
	}

	protected static void InvokeCmdCmdDrinkUpdate(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdDrinkUpdate called on client.");
		}
		else
		{
			((CharacterLiving)obj).CmdDrinkUpdate((byte)reader.ReadPackedUInt32());
		}
	}

	protected static void InvokeCmdCmdHealthUpdate(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdHealthUpdate called on client.");
		}
		else
		{
			((CharacterLiving)obj).CmdHealthUpdate((byte)reader.ReadPackedUInt32());
		}
	}

	public void CallCmdEatUpdate(byte num)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdEatUpdate called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdEatUpdate(num);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdEatUpdate);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.WritePackedUInt32(num);
		SendCommandInternal(networkWriter, 0, "CmdEatUpdate");
	}

	public void CallCmdDrinkUpdate(byte num)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdDrinkUpdate called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdDrinkUpdate(num);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdDrinkUpdate);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.WritePackedUInt32(num);
		SendCommandInternal(networkWriter, 0, "CmdDrinkUpdate");
	}

	public void CallCmdHealthUpdate(byte num)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdHealthUpdate called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdHealthUpdate(num);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdHealthUpdate);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.WritePackedUInt32(num);
		SendCommandInternal(networkWriter, 0, "CmdHealthUpdate");
	}

	static CharacterLiving()
	{
		kCmdCmdEatUpdate = 1909013519;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterLiving), kCmdCmdEatUpdate, InvokeCmdCmdEatUpdate);
		kCmdCmdDrinkUpdate = 1831281103;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterLiving), kCmdCmdDrinkUpdate, InvokeCmdCmdDrinkUpdate);
		kCmdCmdHealthUpdate = 1655342519;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterLiving), kCmdCmdHealthUpdate, InvokeCmdCmdHealthUpdate);
		NetworkCRC.RegisterBehaviour("CharacterLiving", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		if (forceAll)
		{
			writer.WritePackedUInt32(Hungry);
			writer.WritePackedUInt32(Water);
			return true;
		}
		bool flag = false;
		if ((base.syncVarDirtyBits & 1) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.WritePackedUInt32(Hungry);
		}
		if ((base.syncVarDirtyBits & 2) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.WritePackedUInt32(Water);
		}
		if (!flag)
		{
			writer.WritePackedUInt32(base.syncVarDirtyBits);
		}
		return flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		if (initialState)
		{
			Hungry = (byte)reader.ReadPackedUInt32();
			Water = (byte)reader.ReadPackedUInt32();
			return;
		}
		int num = (int)reader.ReadPackedUInt32();
		if ((num & 1) != 0)
		{
			Hungry = (byte)reader.ReadPackedUInt32();
		}
		if ((num & 2) != 0)
		{
			Water = (byte)reader.ReadPackedUInt32();
		}
	}
}
